package xuegao.study.mall.admin.controller.exam;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import xuegao.study.mall.admin.dto.study.exam.Question;
import xuegao.study.mall.admin.feign.AskServiceClient;
import xuegao.study.mall.common.api.CommonResult;
@RestController
@RequestMapping(value = "/api/v1/questions")
public class QuestionController {
    private final Logger LOGGER = LoggerFactory.getLogger(QuestionController.class);
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AskServiceClient askServiceClient;

    @ApiOperation("题目列表")
    @PostMapping(value = "getAll")
    public CommonResult<JSONObject> getAll(@RequestBody JSONObject data) {
        String body = askServiceClient.getAllQuestions(data);
        LOGGER.info("题目列表查询|data:{}, body:{}", data, body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        return CommonResult.success(jsonObject);
    }
    @ApiOperation("题目详情")
    @PostMapping(value = "/detail/{id}")
    public CommonResult<JSONObject> detail(@PathVariable("id") String id) {
        String body = askServiceClient.getQuestionDetail(id);
        JSONObject jsonObject = JSONObject.parseObject(body);
        return CommonResult.success(jsonObject);
    }

    @ApiOperation("添加题目")
    @PostMapping(value = "/add")
    public CommonResult<String> addOrUpdate(@RequestBody Question question) {
        System.out.println("quest:"+JSONObject.toJSONString(question));
        String body = askServiceClient.createQuestion(question);
        return CommonResult.success(body);
    }

    @ApiOperation("删除题目")
    @PostMapping(value = "/delete/{id}")
    public CommonResult<String> delete(@PathVariable("id") String id) {
        askServiceClient.deleteQuestion(id);
        return CommonResult.success(id);
    }

}
